<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="600px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="108px" :rules="formRules">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入分类名称" />
                </el-form-item>
                <el-form-item label="分类" prop="cid">
                    <el-select v-model="formData.cid" clearable placeholder="请选择分类">
                        <el-option v-for="item in giftCateList" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="普通价格" prop="price">
                    <el-input v-model="formData.price" clearable placeholder="请输入价格"  />
                </el-form-item>
                <el-form-item label="豪华价格" prop="vip_price">
                    <el-input v-model="formData.vip_price" clearable placeholder="请输入价格"  />
                </el-form-item>
                <el-form-item label="尊贵价格" prop="svip_price">
                    <el-input v-model="formData.svip_price" clearable placeholder="请输入价格"  />
                </el-form-item>
                <el-form-item label="图片" prop="cover">
                    <el-input v-model="formData.cover" clearable placeholder="请输入图片" :disabled="true" style="display: none;"/>
                    <material-picker v-model="formData.cover" :limit="1" />
                </el-form-item>
                <el-form-item label="描述" prop="desc">
                    <el-input v-model="formData.desc" clearable placeholder="请输入描述"  />
                </el-form-item>
                <el-form-item label="类型" prop="expire_time">
                    <el-radio-group v-model="formData.type">
                        <el-radio :label="0">普通</el-radio>
                        <el-radio :label="1">打扫</el-radio>
                        <el-radio :label="2">修缮</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="有效期" prop="expire_time">
                    <el-input-number v-model="formData.expire_time" clearable placeholder="请输入有效期" type="number" />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input-number v-model="formData.sort" clearable placeholder="请输入排序" type="number" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="roomEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiGiftAdd, apiGiftEdit, apiGiftDetail, apiGiftCateLists } from '@/api/gift'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { ref, computed, onMounted, reactive, shallowRef } from 'vue'

// 定义配置项接口


// 定义配置对象接口

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑分类' : '新增分类'
})


// 表单数据
const formData = reactive({
    cid: '',
    name: '',
    price: 0,
    vip_price: 0,
    svip_price: 0,
    expire_time: 0,
    sort: 0,
    cover: '',
    desc: '',
    is_recommend: '',
    type: 0,
})

// 表单验证
const formRules = reactive<any>({
    name: [{
        required: true,
        message: '请输入礼物名称',
        trigger: ['blur']
    }],
    cid: [{
        required: true,
        message: '请选择分类',
        trigger: ['blur']
    }],
    price: [{
        required: true,
        message: '请输入普通价格',
        trigger: ['blur']
    }
    // ,{
    //     validator: (rule: any, value: any, callback: any) => {
    //         if (value <= 0) {
    //             callback(new Error('请输入普通价格'))
    //         }
    //         callback()
    //     },
    //     trigger: ['blur']
    // }
    ],
    vip_price: [{
        required: true,
        message: '请输入豪华价格',
        trigger: ['blur']
    }
    // ,{
    //     validator: (rule: any, value: any, callback: any) => {
    //         if (value <= 0) {
    //             callback(new Error('请输入豪华价格'))
    //         }
    //         callback()
    //     },
    //     trigger: ['blur']
    // }
    ],
    svip_price: [{
        required: true,
        message: '请输入尊贵价格',
        trigger: ['blur']
    }
    // ,{
    //     validator: (rule: any, value: any, callback: any) => {
    //         if (value <= 0) {
    //             callback(new Error('请输入尊贵价格'))
    //         }
    //         callback()
    //     },
    //     trigger: ['blur']
    // }
    ],
    expire_time: [{
        required: true,
        message: '请输入有效期',
        trigger: ['blur']
    },{
        validator: (rule: any, value: any, callback: any) => {
            if (value <= 0) {
                callback(new Error('请输入有效期'))
            }
            callback()
        },
        trigger: ['blur']
    }],
    cover: [{
        required: true,
        message: '请上传图片',
        trigger: ['blur']
    }]
})



// 在组件挂载时初始化配置项
onMounted(() => {
    
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    editId.value = data.id
}
const editId = ref(0)

const getDetail = async (row: Record<string, any>) => {
    const data = await apiGiftDetail({
        id: row.id
    })
    editId.value = row.id
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    data.price = Number(data.price)
    data.vip_price = Number(data.vip_price)
    data.svip_price = Number(data.svip_price)
    mode.value == 'edit' 
        ? await apiGiftEdit({...data, id: editId.value}) 
        : await apiGiftAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
    getGiftCateList()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

const giftCateList = ref<any[]>([])
const getGiftCateList = async () => {
    const data = await apiGiftCateLists({
        page_no: 1,
        page_size: 10000,
        status: 1
    })
    giftCateList.value = data.lists.map((item: any) => ({
        label: item.name,
        value: item.id
    }))
}
defineExpose({
    open,
    setFormData,
    getDetail
})
</script>

<style scoped>
.config-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.config-item {
    display: flex;
    gap: 10px;
    align-items: center;
}

.config-key-input {
    width: 200px;
}

.config-value-input {
    flex: 1;
}
</style>
